package faceb.server.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.mysql.jdbc.StringUtils;

import faceb.server.dao.UserDao;
import faceb.server.model.AjaxResponse;
import faceb.server.model.User;
import faceb.server.service.UserService;
import faceb.server.util.CharSetUtil;
import faceb.server.util.MyStringUtils;

@Controller
public class UserController {

	@Resource(name = "userDao")
	private UserDao userDao;

	@Resource(name = "userService")
	private UserService userService;

	@Resource(name = "gson")
	private Gson gson;

	/**
	 * hello页面
	 * 
	 * @return
	 */
	@RequestMapping("/hello")
	private String hello() {
		return "hello";
	}

	/**
	 * 登录页面
	 * 
	 * @return
	 */
	@RequestMapping("/tpl/login")
	private String login(HttpServletRequest req ,HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute("user")!=null)
			resp.sendRedirect("/faceb-cupt/tpl/center");
		return "login";
	}

	/**
	 * 首页
	 * 
	 * @return
	 */
	@RequestMapping("/tpl/index")
	private String index() {
		return "index";
	}

	@RequestMapping("/register")
	public String register() {
		return "register";
	}

	@RequestMapping("/logout")
	private String logout() {
		return "logout";
	}

	/**
	 * 处理注册的页面
	 * 
	 * @param req
	 * @param resp
	 */
	@RequestMapping(value = "/dealregister", method = RequestMethod.GET)
	private void handleRegister(HttpServletRequest req, HttpServletResponse resp) {
		String email = req.getParameter("email");
		String pass = req.getParameter("password");
		userService.register(email, pass, req, resp);
		try {
			User u = userDao.getUserByEmail(email);
			req.getSession().setAttribute("user", u);
			resp.sendRedirect("/faceb-cupt/tpl/center/users/" + u.getU_id());
		} catch (Exception e) {

		}
		return;
	}

	/**
	 * 处理完善信息的页面
	 * 
	 * @param req
	 * @param resp
	 */
	@RequestMapping(value = "/dealcomplete", method = RequestMethod.POST)
	@ResponseBody
	private String handlecomplete(HttpServletRequest req, HttpServletResponse resp) {
		AjaxResponse ar = new AjaxResponse();
		String email = req.getParameter("email");
		String name = req.getParameter("name");
		String realname = req.getParameter("realname");
		String sex = req.getParameter("sex");
		String phone = req.getParameter("phone");
		String qq = req.getParameter("qq");
		String blog = req.getParameter("blog");
		String info = req.getParameter("info");
		if (MyStringUtils.hasNullValue(email, name, realname, sex, phone, qq,
				info, blog)) {
			return CharSetUtil.getRespBodyStr(gson.toJson(ar));
			
		}
		sex = sex.equals("man") ? "m" : "f";

		User u = new User(null, null, sex, name, email, 0L, phone, realname,
				info, qq, blog);
		int result =userDao.completeUser(u);
		ar.setStatus(String.valueOf(result));
		ar.setInfo("SUCCESS");
		u = userDao.getUserByEmail(email);
		req.getSession().setAttribute("user", u);
		return CharSetUtil.getRespBodyStr(gson.toJson(ar));
	}

	/**
	 * 处理登录的页面
	 * 
	 * @param req
	 * @param resp
	 */
	@RequestMapping(value = "/deallogin", method = RequestMethod.GET)
	private void handleLogin(HttpServletRequest req, HttpServletResponse resp) {
		String email = req.getParameter("email");
		String password = req.getParameter("password");

		userService.login(email, password, req, resp);
	}

	/**
	 * 处理登出的页面
	 * 
	 * @param req
	 * @param resp
	 */
	@RequestMapping(value = "/deallogout", method = RequestMethod.GET)
	private void handleLogout(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		req.getSession().removeAttribute("user");
		req.setAttribute("message", "登出成功");
		req.getRequestDispatcher("/WEB-INF/pages/message.jsp").forward(req,
				resp);
	}

	/**
	 * 个人中心
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping("/tpl/center/users/{userid}")
	private ModelAndView showUserInfo(@PathVariable("userid")String userid ,HttpServletRequest req) {
		ModelAndView mv = null;
		User loginUser = (User) req.getSession().getAttribute("user");  //当前登录的用户
		loginUser = userDao.getUserByEmail(loginUser.getU_email()); 
		
		User visitUser = userDao.getUserById(userid);   //访问的用户
		if(visitUser == null)
			return null; 
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("visitUser", visitUser);
		model.put("loginUser",loginUser);
		mv = new ModelAndView("center", model);
		return mv;
	}

	/**
	 * 个人资料
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping("/tpl/profile")
	private ModelAndView showProfile(HttpServletRequest req) {
		ModelAndView mv = null;
		User u = (User) req.getSession().getAttribute("user");
		User user = userDao.getUserByEmail(u.getU_email());
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("u", user);
		mv = new ModelAndView("profile", model);
		return mv;
	}

	@RequestMapping(value = "/users/{userid}", method = RequestMethod.GET)
	@ResponseBody
	public String getUserById(@PathVariable("userid") String userid,
			HttpServletResponse resp) throws UnsupportedEncodingException {

		AjaxResponse ar = new AjaxResponse();

		User u = userDao.getUserById(userid);
		if (u != null)
			u.setU_pass(null);
		String jsonStr = gson.toJson(u);
		ar.setStatus("0");
		ar.setInfo("success");
		ar.setData(jsonStr);

		return CharSetUtil.getRespBodyStr(gson.toJson(ar));
	}
}
